package com.hexinfo.dmpro.data.transfer.common.service.impl;

import cn.hutool.core.util.StrUtil;
import com.hexinfo.dmpro.component.common.entity.TableFieldsEntity;
import com.hexinfo.dmpro.component.common.service.impl.HiveQueryService;
import com.hexinfo.dmpro.component.common.util.DataSourceUtil;
import com.hexinfo.dmpro.data.transfer.common.model.CustomFuncationModel;
import com.hexinfo.dmpro.data.transfer.common.model.CustomFuncationResultModel;
import com.hexinfo.dmpro.data.transfer.common.service.IHiveMetadataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HiveMetadataService implements IHiveMetadataService {

    @Autowired
    private HiveQueryService hiveQueryService;

    @Autowired
    @Qualifier("connPoolHiveTarget")
    private DataSource connPoolHiveTarget;


    @Override
    public List<TableFieldsEntity> queryTableFields(String database, String tableName) {
        String dbAndTableName = database + "." + tableName;
        Connection connection = null;
        try {
            connection = connPoolHiveTarget.getConnection();
            return hiveQueryService.queryTableMetadata(connection, dbAndTableName, dbAndTableName);
        } catch (Exception e) {
            throw new RuntimeException("获取元数据异常: " + e.getMessage(), e);
        } finally {
            DataSourceUtil.close(connection);
        }
    }

    @Override
    public List<CustomFuncationResultModel> funcationTest(CustomFuncationModel customFuncationModel) {
        Connection connection = null;
        ResultSet resultSet = null;
        Statement statement = null;
        List<CustomFuncationResultModel> resultModels = new ArrayList<>();
        try {
            connection = connPoolHiveTarget.getConnection();
            statement = connection.createStatement();
            resultSet = statement.executeQuery(customFuncationModel.getQuerySql());
            while (resultSet.next()) {
                ResultSetMetaData metaData = resultSet.getMetaData();
                CustomFuncationResultModel customFuncationResultModel = new CustomFuncationResultModel();
                for (int i = 1; i <= metaData.getColumnCount(); i++) {
                    if (customFuncationModel.getFieldName().equals(metaData.getColumnName(i))) {
                        customFuncationResultModel.setOldData(resultSet.getString(i));
                    } else {
                        customFuncationResultModel.setNewData(resultSet.getString(i));
                    }
                }
                resultModels.add(customFuncationResultModel);
            }
            return resultModels;
        } catch (Exception e) {
            throw new RuntimeException("获取脱敏后数据样例失败: " + e.getMessage(), e);
        } finally {
            DataSourceUtil.close(connection, statement, resultSet);
        }
    }
}
